<?php
$conn = mysql_pconnect("db2.vsdn.bigtv.com.cn:3316","jscn","smpweb343306",MYSQL_CLIENT_INTERACTIVE);
mysql_select_db("jscn",$conn);
		mysql_query("set names 'utf8'");
		//获取计划列表
		//匹配节目单 1-时间合适 2 是否录制状态为空title
		//增加点播节目
    	$configrows = mysql_query("select configname, configvalue from config");//获取配置信息
        $configs = array();
        while ($row = mysql_fetch_array($configrows)){
         	$configs[$row['configname']] = $row['configvalue'];
        }
		$recsite = $configs["prehttp"]."/";//"http://111.208.56.36/data3/recdata/";
		$rec_path = $configs["rec_path"];//物理路径
		//$rechighsite = $configs["prehttp"];//"http://111.208.56.36/data3/recdata/";
		$planlist = mysql_query("select * from epg_schedule");//清空
		while ($row = mysql_fetch_array($planlist))
		{
			$tablename = $row['tablename'];//表名
			$catid = 80;//$row["catid"];//栏目编号
			$spid = $row['spid'];//spid
			$start = $row['start'];//版权开始时间
			$end = $row['end'];//版权结束时间
			$hstart = $row['hstart'];//版权开始时间
			$hend = $row['hend'];//版权结束时间
			$specials = explode(", ", $row['specialid']);//专题编号
			//$fromtime = time() - (2 * 24 * 60 * 60); 
			$fromtime = date('Y-m-d', time());//当天时间
			$totime =  time() + (1 * 24 * 60 * 60);//第二天时间	
			$totime = date('Y-m-d', $totime);
			$epglist = mysql_query("select * from epg where title like '%".$row['keyword']."%' and isneedsave = 0 and contentid ='".$row['contentid']."' and end< '".$totime."' and end > '".$fromtime."' and hour(start)>='".$hstart."' and  hour(start) < '".$hend."' ");
			while ($rowfield = mysql_fetch_array($epglist))
			{
				$epgid = $rowfield["epgid"];
				$title = $rowfield["title"]."(".$rowfield["start"].")";
				//插入数据
				//$insertepgmark = "";				
				$updatesql = "update epg set isneedsave = 1 where epgid = '".$rowfield["epgid"]."' ";
		    	//插入点播内容，初始状态为草稿
		    	//$insertcontentdata = array($catid,$title,$title,2,0,'system',time(),time(),0,1,$tablename);  
		    	$insertcontentsql = "insert into content (catid, title, keywords, status, userid, username, inputtime,updatetime, isonline, contenttype,tablename,template,dateorder) values 
		    		('".$catid."','".$title."','".$title."','2','0','system','".time()."','".time()."','0','1','".$tablename."','show_record','".$rowfield["start"]."')";
		    	mysql_query("begin");
				try {
					mysql_query($updatesql);
				    mysql_query($insertcontentsql);
				    $result = mysql_query("SELECT LAST_INSERT_ID() as id");
				    $lastinsertObj = mysql_fetch_object($result);
				    $contentid = $lastinsertObj->id;
				    //$title = $rowfield["title"];
				   			    
				    $subpath = (int)($contentid / 500);
				    $prepath = $spid."/$subpath/$contentid";  //存储路径 	
				    $realpath = $rec_path."/".$prepath."_medium.flv";				    
				    $insertextendsql = "insert into $tablename(contentid, content, urllow, urlmedium, urlhigh, vodtype, mediatype, starttime, endtime) values 
				    	('$contentid', '$title','".$recsite.$prepath."_low.flv' , '".$recsite.$prepath."_medium.flv', '".$recsite.$prepath."_high.flv', '2', '0', '$start', '$end')";	    
				    mysql_query($insertextendsql);
				    mysql_query("update content set realpath = '$realpath' where contentid = '$contentid'");//更新realpath
				    //专题处理开始
				    $result = mysql_query("SELECT LAST_INSERT_ID() as id");
				    $lastinsertObj = mysql_fetch_object($result);
				    $contentid = $lastinsertObj->id;				    
					foreach($specials as $special)
				    {
				    	$sql = "insert into special_content (specialid, contentid, listorder, contenttype, dateorder) values ('".$special."', '".$contentid."', '".$contentid."', '2','".$rowfield["start"]."')";
				    	mysql_query($sql);
				    }
				    //专题处理结束
				    $updatesql = "update epg set savecontentid = '$contentid',savepath = '$prepath', isneedsave = 1 where epgid = '$epgid' ";
				    $insertsql = "insert into epgmark ( epgid, contentid, title, img, description, start, end, isneedsave, savestatus, savecontentid, savepath)
					select epgid, contentid, title, img, description, start, end, 1, '0', '$contentid', '$prepath' from epg where epgid = '$epgid'";
				    
				    mysql_query($updatesql);
				    mysql_query($insertsql);//插入数据
				    
				    mysql_query("commit");
				} catch (Exception $e) {
					mysql_query("rollback");
					return false;
				}
			}
		}		
		mysql_close($conn);exit;